package org.jetlinks.pro.datasource;

import reactor.core.Disposable;
import reactor.core.publisher.Mono;

import java.util.function.BiConsumer;
import java.util.function.BiFunction;

public class MockDataSourceConfigManager implements DataSourceConfigManager {

    DataSourceConfig properties;
    private BiFunction<ConfigState, DataSourceConfig, Mono<Void>> callback;

    public void setProperties(ConfigState state, DataSourceConfig properties) {
        if (state == ConfigState.disabled) {
            this.properties = null;
        } else {
            this.properties = properties;
        }
        callback.apply(state, properties)
                .subscribe();
    }

    @Override
    public Mono<DataSourceConfig> getConfig(String typeId, String datasourceId) {
        if (properties != null && typeId.equals(properties.getTypeId()) && datasourceId.equals(properties.getId())) {
            return Mono.just(properties);
        }
        return Mono.empty();
    }

    @Override
    public Disposable doOnConfigChanged(BiFunction<ConfigState, DataSourceConfig, Mono<Void>> callback) {
        if (this.callback == null) {
            this.callback = callback;
        }
        return () -> {
        };
    }
}


